//
//  MainViewController.m
//  Supervisor
//
//  Created by 小宇 on 2017/5/10.
//  Copyright © 2017年 北京怀教网络科技股份有限公司. All rights reserved.
//

#import "MainViewController.h"
#import "MainCollectionViewCell.h"
#import "NoticeViewController.h"
#import "QuotedSupervisorViewController.h"
#import "SupecoalAUpervisorMainVC.h"
#import "LZYNetworkHelper.h"
#import "User.h"
#define itemHeight 114

#define indentifier @"MainCollectionViewCell"
@interface MainViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong)UIImageView *headImgView;
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic, nonnull,strong)NSArray *imgNameArr;
@property (nonatomic, nonnull,strong)NSArray *titleNameArr;
@property (nonatomic, nonnull,copy)NSString *titleNotRead;
@end

@implementation MainViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setNavigationBack:[UIImage imageWithColor:[UIColor whiteColor]]];
    NSDictionary *parametersDic = @{@"accessToken":[[User shareInstance]getUserAccessToken]};
    [LZYNetworkHelper GET:noticenum parameters:parametersDic success:^(id responseObject) {
        self.titleNotRead = [NSString stringWithFormat:@"%@",responseObject];
        [self.collectionView reloadData];
    } failure:^(NSError *error) {
    }showHub:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self set_Title:@"教育督导室工作平台"];
    self.view.backgroundColor = BackgroundColor;
    self.edgesForExtendedLayout = UIAccessibilityTraitNone;
    [self.view addSubview:self.headImgView];
    [self.view addSubview:self.collectionView];
}

- (UICollectionView *)collectionView{
    if (_collectionView == nil) {
        UICollectionViewFlowLayout *flowLayot = [UICollectionViewFlowLayout new];
        flowLayot.itemSize = CGSizeMake(75, itemHeight);
        flowLayot.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15);
        flowLayot.minimumInteritemSpacing = kImageCollectionCell_Width;
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.headImgView.frame), Main_Screen_Width, itemHeight) collectionViewLayout:flowLayot];
        [_collectionView registerClass:[MainCollectionViewCell class] forCellWithReuseIdentifier:indentifier];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
    }
    return _collectionView;
}

- (NSArray *)imgNameArr{
    if (!_imgNameArr) {
        _imgNameArr = @[@"notice",@"QuotedSupervisor",@"SpecialSupervisor"];
    }
    return _imgNameArr;
}

- (NSArray *)titleNameArr{
    if (!_titleNameArr) {
        _titleNameArr = @[@"通知公告",@"挂牌督导",@"专项督导"];
    }
    return _titleNameArr;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.titleNameArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    MainCollectionViewCell *mainCollectionViewCell = (MainCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:indentifier forIndexPath:indexPath];
    mainCollectionViewCell.imgView.image = GetImage(self.imgNameArr[indexPath.item]);
    mainCollectionViewCell.nameLabel.text = self.titleNameArr[indexPath.item];
    if (indexPath.item == 0) {
        [mainCollectionViewCell addTipNumLabelWithTipNum:self.titleNotRead];
    }else{
        mainCollectionViewCell.tipNumLabel.hidden = YES;
    }
    return mainCollectionViewCell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.item) {
        case 0:
            [self.navigationController pushViewController:[NoticeViewController new] animated:YES];
            break;
        case 1:
            [self.navigationController pushViewController:[QuotedSupervisorViewController new] animated:YES];
            break;
        case 2:
            [self.navigationController pushViewController:[SupecoalAUpervisorMainVC new] animated:YES];
            break;
        default:
            break;
    }
}


- (UIImageView *)headImgView{
    if (_headImgView == nil) {
        _headImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 5, Main_Screen_Width,GetViewHeight(self.view) * 0.2)];
        _headImgView.image = GetImage(@"mainHead");
    }
    return _headImgView;
}

-(void)set_Title:(NSString *)title
{
    UILabel *navTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 120, 44)];
    navTitleLabel.textColor = MainFontColor;
    navTitleLabel.font = [UIFont boldSystemFontOfSize:17];
    navTitleLabel.text = title;
    navTitleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = navTitleLabel;
}

-(void)setNavigationBack:(UIImage*)image
{
    [self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
    [self.navigationController.navigationBar setBackIndicatorTransitionMaskImage:image ];
    [self.navigationController.navigationBar setShadowImage:image];
}

@end
